﻿
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace NB.MGJ.Common
{
    public class WeiXinHelper
    {
        /// <summary>
        /// 验证token
        /// </summary>
        /// <param name="token"></param>
        public static void CheckToken(string token)
        {
            string signature = HttpContext.Current.Request["signature"] + "";
            string timestamp = HttpContext.Current.Request["timestamp"];
            string nonce = HttpContext.Current.Request["nonce"];
            string echostr = HttpContext.Current.Request["echostr"];

            //验证token
            WriteLog("验证token", signature + "," + timestamp + "," + nonce + "," + echostr);
            if (signature != "")
            {
                if (CheckSignature.Check(signature, timestamp, nonce, token))
                {
                    HttpContext.Current.Response.Write(echostr);
                    HttpContext.Current.Response.End();
                }
            }
        }


        /// <summary>
        /// 微信OAuth2.0授权
        /// </summary>
        public static string OAuthWeixin(string appid, string redirecturl)
        {
            //授权获取用户的信息
            var result = "";
            try
            {
                
                result = OAuthApi.GetAuthorizeUrl(appid, redirecturl, "666", OAuthScope.snsapi_userinfo);

            }
            catch (Exception e)
            {

                WriteLog("text", "text:" + "授权出错:" + e.Message);
            }
            return result;
        }

        /// <summary>
        /// http请求
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="postDataStr"></param>
        /// <returns></returns>
        public static string HttpPost(string Url, string postDataStr)
        {

            HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(Url);
            webrequest.Method = "post";

            byte[] postdatabyte = Encoding.UTF8.GetBytes(postDataStr);
            webrequest.ContentLength = postdatabyte.Length;
            Stream stream;
            stream = webrequest.GetRequestStream();
            stream.Write(postdatabyte, 0, postdatabyte.Length);
            stream.Close();
            using (var httpWebResponse = webrequest.GetResponse())
            using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
            {

                String ret = responseStream.ReadToEnd();
                return ret;
            }
        }

        public static void WriteLog(string logName, string logContent)
        {
            //HttpContext.Current.Server.MapPath("Logs/");
            string LogSaveUrl = HttpContext.Current.Server.MapPath("/") + "Logs/";

            if (Directory.Exists(LogSaveUrl) == false) Directory.CreateDirectory(LogSaveUrl);

            StreamWriter dout = new StreamWriter(LogSaveUrl + logName + ".txt", true);
            dout.Write("\r\n事件：" + logContent + "\r\n操作时间：" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));
            dout.Close();
        }
    }

}